package interfazeak;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.sql.Date;
import java.sql.ResultSet;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Vector;
import java.util.jar.*;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

import laguntzaileak.Botikaaukeratua;

import com.toedter.calendar.IDateEditor;
import com.toedter.calendar.JCalendar;
import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.JDayChooser;

import db.BotikaKud;
import db.DBKudeatzailea;

public class GehituBotikak extends JDialog {
	private Botikak guraso;
	private final JPanel contentPanel = new JPanel();
	private JLabel tratamendulabel;
	private JLabel izurrilabel;
	private Component comboBox;
	private JButton lekua;
	private JTextField botikatxt;
	private JTextField gaixoatxt;
	private JLabel data;
	private JCalendar cal;
	private JButton gorde;
	private JTextField datatxt;
	private java.util.Date fetxa;
	private Botikaaukeratua botikabektore;
	private JButton atzera;

	public static void main(String[] args) {
		try {
			final Boolean MODALA = true;
			GehituBotikak dialog = new GehituBotikak(new Botikak(), MODALA);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public GehituBotikak(Botikak guraso, Boolean modala) {
		this.guraso = guraso;
		botikabektore = new Botikaaukeratua(null, null, null);
		setBounds(200, 200, 600, 350);
		setTitle("GEHITU BOTIKAK");
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		tratamendulabel = new JLabel("Tratamendua:");
		tratamendulabel.setBounds(30, 40, 100, 15);
		contentPanel.add(tratamendulabel);

		izurrilabel = new JLabel("Izurria:");
		izurrilabel.setBounds(30, 80, 100, 15);
		contentPanel.add(izurrilabel);

		botikatxt = new JTextField();

		botikatxt.setBounds(130, 40, 114, 19);
		contentPanel.add(botikatxt);
		botikatxt.setColumns(10);

		gaixoatxt = new JTextField();
		gaixoatxt.setBounds(130, 80, 114, 19);
		contentPanel.add(gaixoatxt);
		gaixoatxt.setColumns(10);

		data = new JLabel("Data:");
		data.setBounds(320, 40, 70, 15);
		contentPanel.add(data);

		cal = new JCalendar();
		cal.setBounds(320, 70, 240, 200);
		contentPanel.add(cal);
/*
		atzera = new JButton("ATZERA");
		atzera.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				GehituBotikak.this.dispose();
			}
		});
		atzera.setBounds(480, 30, 117, 25);
		contentPanel.add(atzera);*/

		gorde = new JButton("GORDE");
		gorde.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				// berezie gero ezabatu!!!!!!!!!!

				String año = Integer.toString(cal.getCalendar().get(
						java.util.Calendar.YEAR));
				String mes = Integer.toString(cal.getCalendar().get(
						java.util.Calendar.MONTH) + 1);
				String dia = Integer.toString(cal.getCalendar().get(
						java.util.Calendar.DATE));

				String eguna = (dia + "-" + mes + "-" + año);
				String gaitza = lortugaitza();
				String trat = lortutratamendua();

				if (gaitza.equals("") || trat.equals("") || eguna.equals("")) {
					JOptionPane.showMessageDialog(GehituBotikak.this,
							"Eremu guztiak bete", "KONTUZ",
							JOptionPane.WARNING_MESSAGE);
				} else {
					botikabektore.setFetxa(eguna);
					botikabektore.setIzurria(gaitza);
					botikabektore.setTratamendua(trat);

					GehituBotikak.this.guraso.sartuBotika(botikabektore);
					GehituBotikak.this.dispose();
					GehituBotikak.this.guraso.ezabatutaula();
					GehituBotikak.this.guraso.bistaratu();
				}

			}
		});
		gorde.setBounds(270, 280, 117, 25);
		contentPanel.add(gorde);

	}

	protected String lortugaitza() {
		String gaitz;
		gaitz = gaixoatxt.getText();
		return gaitz;
	}

	protected String lortutratamendua() {
		String trat;
		trat = botikatxt.getText();
		return trat;
	}
}
